package cn.zymall.order.controller;

import cn.zymall.common.controller.crud.BaseController;
import cn.zymall.common.model.Order;
import cn.zymall.order.config.WxPayConfig;
import cn.zymall.order.service.AliPayService;
import cn.zymall.order.service.KdniaoQuerySetvice;
import cn.zymall.order.service.OrderService;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.Map;

/**
 * @program: mall
 * @description: 订单控制器
 * @author: YueNiao
 * @email: 1486617253@qq.com
 * @create: 2020-08-17 22:09
 **/
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController<OrderService, Order> {
	@Autowired
	OrderService orderService;
	@Autowired
	AliPayService aliPayService;

	@Autowired
	WxPayConfig wxPayConfig;

	@Autowired
	KdniaoQuerySetvice kdniaoQuerySetvice;
	/**
	 * 创建初始订单
	 *
	 * @param order
	 * @return 下单是否成功
	 * @throws IOException
	 */
	@PostMapping("/createOrder")
	public boolean decrementProductStore(@RequestBody Order order) throws IOException {
		System.out.println(order);
		return orderService.decrementProductStore(order) == null ? false : true;
	}

	;

	/**
	 * 支付宝支付
	 *
	 * @param request
	 * @param response
	 * @param order
	 */
	@PostMapping("/aliToPay")
	public void aliToPay(HttpServletRequest request, HttpServletResponse response, @RequestBody Order order) throws IOException {
		aliPayService.toPay(request, response, order);
	}

	/**
	 * 阿里支付回调
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@GetMapping("/alipayNotify")
	public void alipayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		aliPayService.alipayNotify(request, response);
	}

	/**
	 * 查询是否已支付
	 * @param id 商城订单
	 * @return
	 * @throws AlipayApiException
	 */
	@RequestMapping("/searchOrder/{id}")
	public boolean searchOrder(@PathParam("id") String id) throws AlipayApiException {
		return aliPayService.search(id);
	}

	/**
	 * 动态查询
	 * @param userId
	 * @param orderStatus
	 * @return
	 */
	@GetMapping("/queryOrder")
	public PageInfo<Order> queryOrder(
								@RequestParam(required = false) String userId,
	                           @RequestParam(required = false) String orderStatus,
								int page ,int rows){
		return orderService.queryOrder(userId,orderStatus,page,rows);
	}

	/**
	 * 商家查询
	 * @param merchantId
	 * @param orderStatus
	 * @param page
	 * @param rows
	 * @return
	 */
	@GetMapping("/merchantQueryOrder")
	public PageInfo<Order> merchantQueryOrder(
			@RequestParam(required = false) String merchantId,
			@RequestParam(required = false) String orderStatus,
			int page ,int rows){
		System.out.println(merchantId);
		return orderService.merchantQueryOrder(merchantId,orderStatus,page,rows);
	}

	/**
	 * 模糊查询
	 * @param userId
	 * @param orderStatus
	 * @param page
	 * @param rows
	 * @param value
	 * @return
	 */
	@GetMapping("/dimQueryOrder")
	public PageInfo<Order> dimQueryOrder(
			@RequestParam(required = false) String userId,
			@RequestParam(required = false) String orderStatus,
			int page ,int rows,String value){
		return orderService.dimQueryOrder(userId,orderStatus,value,page,rows);
	}

	/**
	 * 店家模糊查询
	 * @param merchantId
	 * @param orderStatus
	 * @param page
	 * @param rows
	 * @param value
	 * @return
	 */
	@GetMapping("/dimMerchantQueryOrder")
	public PageInfo<Order> dimMerchantQueryOrder(
			@RequestParam(required = false) String merchantId,
			@RequestParam(required = false) String orderStatus,
			int page ,int rows,String value){
		return orderService.dimMerchantQueryOrder(merchantId,orderStatus,value,page,rows);
	}
	/**
	 * 发货
	 * @param order
	 * @return
	 * @throws Exception
	 */
	@PutMapping("/sendKd")
	public boolean sendKd(@RequestBody Order order) throws Exception {
		System.out.println(order);
		String s = kdniaoQuerySetvice.kdQueryexist(order.getLogisticslId());
		System.out.println(s);
		Object parse = JSON.parse(s);
		Map maps = (Map)parse;
		System.out.println(maps.get("Code"));
		if("100".equals(maps.get("Code"))){
			order.setOrderStatus((byte)3);
			orderService.updateById(order);
			return true;
		}
		return false;
	}
}
